﻿/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: NGUYEN VAN TY
Created Date	: 12/01/2010
Modified By     :
Modified Date   :
Note            :

*/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoCategory
    {
        public daoCategory()
        {

        }
        public CategoryCollection GetByParentId(int parentId)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            CategoryCollection items = new CategoryCollection();
            try
            {               
                para[0] = new SqlParameter(busCategory.PARENTID, SqlDbType.Int);
                para[0].Value = parentId;
                de = clsSQL.SPDataReader("Category_GetByParentId", para);
                while (de.Read())
                {
                    Category item = Load(de);
                    items.Add(item);
                }

                de.Close();               
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoCategory", "GetByParentId(int parentId)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }


        public CategoryCollection List(Category category)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            CategoryCollection items = new CategoryCollection();

            try
            {
                para[0] = new SqlParameter(busCategory.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = category.OrderBy;
                para[1] = new SqlParameter(busCategory.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = category.OrderDirection;
                para[2] = new SqlParameter(busCategory.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = category.CurrentPage;
                para[3] = new SqlParameter(busCategory.PAGESIZE, SqlDbType.Int);
                para[3].Value = category.PageSize;
                para[4] = new SqlParameter(busCategory.CONDITION, SqlDbType.NVarChar);
                para[4].Value = category.Condition;
                para[5] = new SqlParameter(busCategory.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("Category_List", para);
                while (de.Read())
                {
                    Category item = Load(de);
                    items.Add(item);
                }

                de.Close();
                category.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoCategory", "List(Category Category)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }

        public Category Detail(Category _product)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            Category item = new Category();

            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = _product.CategoryID;
                using (de = clsSQL.SPDataReader("Category_Detail", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoCategory", "Detail(Category category)", ex.ToString());
            }
            finally
            {
                de = null;

                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }


        private Category Load(IDataReader de)
        {
            Category item = new Category();
            //item.CategoryID = clsConvert.Int(clsString.CheckObject(de["CategoryID"]));
            item.CategoryID = clsConvert.DbToInt("CategoryID", de);
            item.Name = clsConvert.DbToString("Name", de);
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.Description = clsConvert.DbToString("Description", de);
            item.PageTitle = clsConvert.DbToString("PageTitle", de);
            item.MetaKeywords = clsConvert.DbToString("MetaKeywords", de);
            item.MetaDescription = clsConvert.DbToString("MetaDescription", de);
            item.IsActive = clsConvert.DbToBoolean("IsActive", de);
            item.Level = clsConvert.DbToByte("Level", de);
            item.ParentId = clsConvert.DbToInt("ParentId", de);
            item.SortOrder = clsConvert.DbToInt("SortOrder", de);
            item.UpdatedBy = clsConvert.DbToString("UpdatedBy", de);
            item.UpdatedDate = clsConvert.DbToDateTime("UpdatedDate", de);
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }

        private Category LoadCateForProduct(IDataReader de)
        {
            Category item = new Category();
           
            item.CategoryID = clsConvert.DbToInt("CategoryID", de);
            item.Name = clsConvert.DbToString("Name", de);        
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }

        /// <summary>
        /// Move item up
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public bool OrderUp(int categoryID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];           
            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = categoryID;
                result = clsSQL.SPExecute("Category_OrderUp", para);              

            }
            catch (Exception ex)
            {                
                clsFile.WriteLog("daoCategory", "Category_OrderUp(Category category)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        /// <summary>
        /// Move item down
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public bool OrderDown(int categoryID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];          
            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = categoryID;
               result = clsSQL.SPExecute("Category_OrderDown", para);
               

            }
            catch (Exception ex)
            {               
                clsFile.WriteLog("daoCategory", "Category_OrderDown(Category category)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Active(int categoryID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];         
            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = categoryID;
                result = clsSQL.SPExecute("Category_Active", para);
               
            }
            catch (Exception ex)
            {                
                clsFile.WriteLog("daoCategory", "Category_Active(Category category)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;           
        }
        public bool DeActive(int categoryID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];           
            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = categoryID;
                result = clsSQL.SPExecute("Category_DeActive", para);    
            }
            catch (Exception ex)
            {             
                clsFile.WriteLog("daoCategory", "Category_DeActive(Category category)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Delete(int categoryID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];         
            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = categoryID;
                result = clsSQL.SPExecute("Category_Delete", para);
            }
            catch (Exception ex)
            {               
                clsFile.WriteLog("daoCategory", "Category_Delete(Category category)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Insert(Category objCategory)
        {
         
            int result=0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[10];        
            try
            {
                para[0] = new SqlParameter(busCategory.PARENTID, SqlDbType.Int);
                para[0].Value = clsConvert.IntToDB(objCategory.ParentId);

                para[1] = new SqlParameter(busCategory.ISACTIVE, SqlDbType.Bit);
                para[1].Value = objCategory.IsActive;

                para[2] = new SqlParameter(busCategory.NAME, SqlDbType.NVarChar);
                para[2].Value = objCategory.Name;

                para[3] = new SqlParameter(busCategory.DESCRIPTION, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objCategory.Description);

                para[4] = new SqlParameter(busCategory.CREATEDBY, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objCategory.CreatedBy);

                para[5] = new SqlParameter(busCategory.CREATEDDATE, SqlDbType.DateTime);
                para[5].Value = objCategory.CreatedDate;

                para[6] = new SqlParameter(busCategory.PageTitle, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objCategory.PageTitle);
                para[7] = new SqlParameter(busCategory.MetaKeywords, SqlDbType.VarChar);
                para[7].Value = clsConvert.StringToDB(objCategory.MetaKeywords);
                para[8] = new SqlParameter(busCategory.MetaDescription, SqlDbType.VarChar);
                para[8].Value = clsConvert.StringToDB(objCategory.MetaDescription);

                para[9] = new SqlParameter(busCategory.Type, SqlDbType.TinyInt);
                para[9].Value = objCategory.Type;

                result = clsSQL.SPExecute("Category_Insert", para);               

            }
            catch (Exception ex)
            {               
                clsFile.WriteLog("daoCategory", "Category_Insert(Category category)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public bool Update(Category objCategory)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[10];         
            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = objCategory.CategoryID;

                para[1] = new SqlParameter(busCategory.ISACTIVE, SqlDbType.Bit);
                para[1].Value = objCategory.IsActive;

                para[2] = new SqlParameter(busCategory.NAME, SqlDbType.NVarChar);
                para[2].Value = objCategory.Name;

                para[3] = new SqlParameter(busCategory.DESCRIPTION, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objCategory.Description);

                para[4] = new SqlParameter(busCategory.UPDATEDBY, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objCategory.UpdatedBy);

                para[5] = new SqlParameter(busCategory.UPDATEDDATE, SqlDbType.DateTime);
                para[5].Value = objCategory.UpdatedDate;


                para[6] = new SqlParameter(busCategory.PageTitle, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objCategory.PageTitle);
                para[7] = new SqlParameter(busCategory.MetaKeywords, SqlDbType.VarChar);
                para[7].Value = clsConvert.StringToDB(objCategory.MetaKeywords);
                para[8] = new SqlParameter(busCategory.MetaDescription, SqlDbType.VarChar);
                para[8].Value = clsConvert.StringToDB(objCategory.MetaDescription);              
                para[9] = new SqlParameter(busCategory.PARENTID, SqlDbType.Int);
                para[9].Value =clsConvert.IntToDB(objCategory.ParentId);
                result = clsSQL.SPExecute("Category_Update", para);
               

            }
            catch (Exception ex)
            {              
                clsFile.WriteLog("daoCategory", "Category_Insert(Category category)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public Category GetByID(int _categoryID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            Category item = new Category();

            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = _categoryID;
                using (de = clsSQL.SPDataReader("Category_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoCategory", "GetByID(Category category)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }
        public Category GetByProductID(int _productID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            Category item = new Category();

            try
            {
                para[0] = new SqlParameter("ProductId", SqlDbType.Int);
                para[0].Value = _productID;
                using (de = clsSQL.SPDataReader("Category_GetByProductID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoCategory", "GetByProductID(int _productID)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }
        public CategoryCollection GetAllToControl()
        {
            clsSQL clsSQL = new clsSQL();          
            SqlDataReader de;
            CategoryCollection items = new CategoryCollection();
            try
            {
                de = clsSQL.SPDataReader("Category_GetAllToControl");
                while (de.Read())
                {
                    Category item = Load(de);
                    items.Add(item);
                }
                de.Close();
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoCategory", "GetAllToControl(Category Category)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }
        public Category GetCateForProduct(int categoryID,int productID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];
            SqlDataReader de;
            Category item = new Category();

            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = clsConvert.IntToDB(categoryID);
                para[1] = new SqlParameter(busProduct.PRODUCTID, SqlDbType.Int);
                para[1].Value = productID;
                using (de = clsSQL.SPDataReader("Category_GetCateForProduct", para))
                {
                    while (de.Read())
                    {
                        item = LoadCateForProduct(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoCategory", "GetCateForProduct(Category category)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }
        public string GetCategoryPathId(int categoryID)
        {
            string result = string.Empty;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = categoryID;
                SqlDataReader reader=clsSQL.SPDataReader("Category_GetCategoryPathId", para);
                if (reader.Read())
                    result = reader.GetValue(0).ToString();
                else result ="/" + categoryID;
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCategory", "GetCategoryPathId(int categoryID)", ex.ToString());
                result = "/" + categoryID;
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public void DeleteVideoCategoryTree(int categoryID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = categoryID;
                result = clsSQL.SPExecute("Category_DeleteVideoCategoryTree", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCategory", "DeleteVideoCategoryTree(int categoryID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }           
        }
        public void DeleteProductCategoryTree(int categoryID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = categoryID;
                result = clsSQL.SPExecute("Category_DeleteProductCategoryTree", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCategory", "DeleteProductCategoryTree(int categoryID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
        }
        public void DeleteAreaCategoryTree(int categoryID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = categoryID;
                result = clsSQL.SPExecute("Category_DeleteAreaCategoryTree", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCategory", "DeleteAreaCategoryTree(int categoryID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
        }
    }
}
